﻿/*
 * 文件名称：
 * 程序员：huangsb
 * 创建时间：2008/05/20
 * 
 * 功能说明：栏目权限管理
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Role_ColumnRole : System.Web.UI.Page
{
    public string RoleID = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        if (RoleHelper.AdminCache.typeID != 1)
        {
            JsHelper.redirect(Page, "当前用户不是管理员，无法继续操作！", "../../Main.aspx");
        }

        RoleID = Whir.Common.WebHelper.NetRequest.SafeRequest(Page, "id");

        if (!IsPostBack)
        {
            //获取权限            
            if (RoleID != "")
            {
                BindTree(this.TreeView1, "导航栏目", "select columnID,columnName,isUse from Whir_Column where parentID=", "0", " order by sort");    //注意SQL语句中父ID的衔接和显示字段的次序
            }
            else
            {
                Response.Redirect("RoleList.aspx");
            }
        }
    }

    private Whir.Model.Whir_AdminRole GetRole(string RoleID)
    {
        Whir.SQLServerDAL.Whir_AdminRole bll = new Whir.SQLServerDAL.Whir_AdminRole();
        Whir.Model.Whir_AdminRole model = bll.GetModel(int.Parse(RoleID));
        return model;
    }

    private void BindTree(TreeView tv, string title, string sql, string pid, string sqlOrder)
    {
        Whir.Model.Whir_AdminRole Role = GetRole(RoleID);

        //初始化顶部信息
        this.tdName.InnerText = Role.roleName;
        this.tdDesc.InnerText = Role.roleDesc;
        //end

        tv.Attributes.Add("onclick", "OnTreeNodeChecked(event, '" + tv.ClientID + "')");

        TreeNode node = new TreeNode();
        node.Text = title;
        node.Value = "0";
        node.PopulateOnDemand = false;
        node.SelectAction = TreeNodeSelectAction.Expand;
        node.ShowCheckBox = true;    //定义为CheckBox节点
        if (Role.columnIDs.IndexOf("0") != -1)
        {
            node.Checked = true;
        }
        TreeNode nodeItem;

        DataSet ds = Whir.SQLServerDAL.DbHelperSQL.Query(sql + pid + sqlOrder);
        if (ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) return;

        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            nodeItem = new TreeNode();
            nodeItem.Text = ds.Tables[0].Rows[i][1].ToString();
            
            //是否要求详细设置
            //if (ds.Tables[0].Rows[i][2].ToString() == "True")
            //{
            //    nodeItem.Text += "&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>『<a href='PageRole.aspx?rid=" + this.RoleID + "&cid=" + ds.Tables[0].Rows[i][0].ToString() + "' style='color:red;'>详细设置</a>』</font>";
            //}
            

            nodeItem.Value = ds.Tables[0].Rows[i][0].ToString();

            if (Role.columnIDs.IndexOf("," + nodeItem.Value + ",") >= 0)
            {
                nodeItem.Checked = true;
            }

            nodeItem.PopulateOnDemand = false;
            nodeItem.SelectAction = TreeNodeSelectAction.Expand;
            nodeItem.Expanded = false;
            nodeItem.ShowCheckBox = true;
            BindNode(nodeItem, sql, ds.Tables[0].Rows[i][0].ToString(), sqlOrder);
            node.ChildNodes.Add(nodeItem);
        }
        tv.Nodes.Add(node);
    }

    private void BindNode(TreeNode node, string sql, string pid, string sqlOrder)
    {
        Whir.Model.Whir_AdminRole Role = GetRole(RoleID);

        TreeNode nodeItem;
        DataSet ds = null;
        ds = Whir.SQLServerDAL.DbHelperSQL.Query(sql + pid + sqlOrder);
        if (ds == null) return;
        if (ds.Tables[0].Rows.Count == 0) return;

        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            nodeItem = new TreeNode();
            nodeItem.Text = ds.Tables[0].Rows[i][1].ToString();
            //是否要求详细设置
            //if (ds.Tables[0].Rows[i][2].ToString() == "True")
            //{
            //    nodeItem.Text += "&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>『<a href='PageRole.aspx?rid=" + this.RoleID + "&cid=" + ds.Tables[0].Rows[i][0].ToString() + "' style='color:red;'>详细设置</a>』</font>";
            //}

            nodeItem.Value = ds.Tables[0].Rows[i][0].ToString();
            if (Role.columnIDs.IndexOf("," + nodeItem.Value + ",") >= 0)
            {
                nodeItem.Checked = true;
            }

            nodeItem.PopulateOnDemand = false;
            nodeItem.SelectAction = TreeNodeSelectAction.Expand;
            nodeItem.ShowCheckBox = true;
            BindNode(nodeItem, sql, ds.Tables[0].Rows[i][0].ToString(), sqlOrder);
            node.ChildNodes.Add(nodeItem);
        }
    }

    //提交
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string ColumnID = ",";
        //综合部分
        foreach (TreeNode chk in TreeView1.CheckedNodes)
        {
            ColumnID += chk.Value + ",";
        }

        Whir.Model.Whir_AdminRole Role = GetRole(RoleID);
        Role.columnIDs = ColumnID;
        new Whir.SQLServerDAL.Whir_AdminRole().Update(Role);

        Response.Redirect("RoleList.aspx");
    }
}
